package com.iknight.theory.controller;

import com.github.pagehelper.PageInfo;
import com.iknight.mgr.core.base.BaseController;
import com.iknight.mgr.core.constant.IkgConstantCode;
import com.iknight.mgr.core.utils.IkgStringUtils;
import com.iknight.mgr.core.utils.IkgWebUtils;
import com.iknight.theory.api.TeTgService;
import com.iknight.theory.model.TETgModel;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author JeryZym
 * @date 2017/12/20
 */
@RestController
@RequestMapping("/rest/tg")
public class TgController implements BaseController<TETgModel> {
    private final static Logger logger;

    static {
        logger = LogManager.getLogger(TgController.class);
    }

    @Autowired
    private TeTgService<TETgModel, String> teTgService;

    @Override
    public TETgModel format(Map<String, Object> params) {
        TETgModel entity = new TETgModel();
        entity.setTg_id(IkgStringUtils.ObjectToString(params.get("tg_id")));
        if (IkgStringUtils.isNotNull(params.get("tg_no"))) {
            String tg_no = IkgStringUtils.ObjectToString(params.get("tg_no"));
            entity.setTg_no(tg_no);
        }
        if (IkgStringUtils.isNotNull(params.get("tg_name"))) {
            String tg_name = IkgStringUtils.ObjectToString(params.get("tg_name"));
            entity.setTg_name(tg_name);
        }
        if (IkgStringUtils.isNotNull(params.get("tg_capacity"))) {
            String value = IkgStringUtils.ObjectToString(params.get("tg_capacity"));
            entity.setTg_capacity(Double.valueOf(value.equals("") ? "0" : value));
        }
        if (IkgStringUtils.isNotNull(params.get("is_effective"))) {
            String is_effective = IkgStringUtils.ObjectToString(params.get("is_effective"));
            entity.setIs_effective(is_effective);
        }

        return entity;
    }

    @Override
    @PostMapping("/save")
    @ApiOperation(value = "保存方法", httpMethod = "POST")
    public Object save(@RequestBody Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
        if (isPKeyNotNull(params)) {
            teTgService.save(format(params));
        } else {
            return IkgWebUtils.postIkgResponse(false, "传入参数为空", IkgConstantCode.PARAMSNULL);
        }
        return IkgWebUtils.postIkgResponse(true, "保存成功!", IkgConstantCode.OPERSUCCESS);
    }

    @Override
    @PostMapping("/delete")
    @ApiOperation(value = "删除方法", httpMethod = "POST")
    public Object delete(@RequestBody Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
        if (isPKeyNotNull(params)) {
            String pKey = IkgStringUtils.ObjectToString(params.get("tg_id"));
            teTgService.delete(pKey);
        } else {
            return IkgWebUtils.postIkgResponse(false, "传入参数为空", IkgConstantCode.PARAMSNULL);
        }
        return IkgWebUtils.postIkgResponse(true, "删除成功!", IkgConstantCode.OPERSUCCESS);
    }

    @Override
    @PostMapping("/selectOne")
    @ApiOperation(value = "单条记录查询方法", httpMethod = "POST")
    public Object selectOne(@RequestBody Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
//        return null;
        if (isPKeyNotNull(params)) {
            String pKey = IkgStringUtils.ObjectToString(params.get("tg_id"));
            TETgModel entity = teTgService.selectOne(pKey);
            return IkgWebUtils.postIkgResponse(true, "查询成功!", entity);
        } else {
            return IkgWebUtils.postIkgResponse(false, "传入参数为空", IkgConstantCode.PARAMSNULL);
        }
    }

    @Override
    @PostMapping("/list")
    @ApiOperation(value = "根据参数分页查询", httpMethod = "POST")
    public Object selectByPage(@RequestBody Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
        if (null == params) {
            return IkgWebUtils.postIkgResponse(false, "传入参数为空", IkgConstantCode.PARAMSNULL);
        }
        int pageIndex = IkgStringUtils.ObjectToInt(params.get("pageNumber"));
        int pageSize = IkgStringUtils.ObjectToInt(params.get("pageSize"));
        PageInfo<TETgModel> pageInfo = teTgService.selectPage(params, pageIndex, pageSize);
        return IkgWebUtils.postResult(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public boolean isPKeyNotNull(Map<String, Object> params) {
        return null != params && IkgStringUtils.isNotNull(params.get("tg_id"));
    }

    @PostMapping("/getTgDict")
    @ApiOperation(value = "获取台区字典数据,全量查询", httpMethod = "POST")
    public Object getTgDictionary(HttpServletRequest request, HttpServletResponse response) {
        List<Map> result = teTgService.getTgDirectionary();
        return IkgWebUtils.postIkgResponse(true, "查询成功", result);
    }
}
